package com.cfuture.xiaozhi.smarthome.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.cfuture.xiaozhi.smarthome.R;
import com.cfuture.xiaozhi.smarthome.ui.contentfragment.DayLightFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

/**
 * 中间主要显示内容的FrameLayout
 * 
 * @author chenjunzhi
 * @date 2013-8-22
 */
public class MainContentActivity extends FragmentActivity implements
		OnClickListener {

	private Fragment mContent;
	private SlidingMenu menu;
	private TextView tvTitle;
	private Button menuToggle;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_content_activity);
		tvTitle = (TextView) findViewById(R.id.main_content_title);
		menuToggle = (Button) findViewById(R.id.main_content_bt_menu_toggle);
		
		
//		if (savedInstanceState != null)
//			mContent = getSupportFragmentManager().getFragment(
//					savedInstanceState, "mContent");
//		else {
			mContent = new DayLightFragment();
			tvTitle.setText("日光灯");
//		}

		// set the Above View
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, mContent).commit();

		initSlidemenu();
		// set the Behind View
		// setBehindContentView(R.layout.menu_frame);
		// getSupportFragmentManager().beginTransaction()
		// .replace(R.id.menu_frame, new LeftMenuFragment()).commit();
		//
		// customize the SlidingMenu
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menuToggle.setOnClickListener(this);
		// menu.toggle();
	}

	private void initSlidemenu() {
		menu = new SlidingMenu(this);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setShadowWidthRes(R.dimen.shadow_width);
		menu.setShadowDrawable(R.drawable.shadow);
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		menu.setMenu(R.layout.menu_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new LeftMenuFragment()).commit();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}

	/**
	 * 改变显示内容
	 * 
	 * @param fragment
	 */
	public void switchContent(Fragment fragment, String title) {
		mContent = fragment;
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, fragment).commit();
		menu.showContent();
		tvTitle.setText(title);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.main_content_bt_menu_toggle:
			menu.toggle();
			break;

		default:
			break;
		}
	}

}
